package cn.telling.product.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.telling.product.dao.ISupplyDao;
import cn.telling.product.vo.SupplyVo;
import cn.telling.utils.AutoInjection;
import cn.telling.utils.BaseUtil;
import cn.telling.utils.StringHelperTools;

/**
 * 
 * @ClassName: SupplyDaoImpl 
 * @Description: 供应商数据服务接口的实现
 * @author 陈赟 
 * @date 2015年6月17日 上午11:25:45 
 *
 */
@Repository("supplyDao")
public class SupplyDaoImpl implements ISupplyDao {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SupplyDaoImpl.class);
    
    @Autowired
    @Qualifier("film-template")
    private JdbcTemplate jdbcTemplate;

    @Override
    public SupplyVo findSupplyByUserId(String userId) {
        String sql = "select id,id supply_id,area_id,user_id,introduction ,name,jc,regcode,corporation,corp_code,taxcode,tel,corpcard_pic,license_pic,tax_pic,organ_pic,pri_name,pri_tel,pri_sex,pri_mail,pri_addr,pri_position,supply_addr,supply_rank,supply_type,supply_cred,account_status,supply_status,remarks,PRI_MOBILE,MOBILE,SEX,deliver_type from supply where del_flag='1' and user_id=? ";
        Object args [] = new Object[]{userId};
        args = StringHelperTools.nullToString(args);
        
        LOGGER.debug("根据用户ID获取供应商信息：" + BaseUtil.logSQL(sql, args));
        List<SupplyVo> supplyList = jdbcTemplate.query(sql,args, new RowMapper<SupplyVo>() {
            public SupplyVo mapRow(ResultSet rs, int rowNum)throws SQLException {
                SupplyVo vo = new SupplyVo();
                AutoInjection.Rs2Vo(rs, vo, null);
                return vo;
            }
        });
        if(supplyList != null && supplyList.size() > 0) {
            return supplyList.get(0);
        }
        return null;
    }

    @Override
    public SupplyVo findSupplyBySupplyId(String supplyId) {
        String sql = "select id,id supply_id,area_id,user_id,introduction ,name,jc,regcode,corporation,corp_code,taxcode,tel,corpcard_pic,license_pic,tax_pic,organ_pic,pri_name,pri_tel,pri_sex,pri_mail,pri_addr,pri_position,supply_addr,supply_rank,supply_type,supply_cred,account_status,supply_status,remarks,PRI_MOBILE,MOBILE,SEX,deliver_type from supply where del_flag='1' and id=? ";
        Object args [] = new Object[]{supplyId};
        args = StringHelperTools.nullToString(args);
        
        LOGGER.debug("根据供应商ID获取供应商信息：" + BaseUtil.logSQL(sql, args));
        List<SupplyVo> supplyList = jdbcTemplate.query(sql,args, new RowMapper<SupplyVo>() {
            public SupplyVo mapRow(ResultSet rs, int rowNum)throws SQLException {
                SupplyVo vo = new SupplyVo();
                AutoInjection.Rs2Vo(rs, vo, null);
                return vo;
            }
        });
        if(supplyList != null && supplyList.size() > 0) {
            return supplyList.get(0);
        }
        return null;
    }

    @Override
    public SupplyVo findSupplyByShopProductId(String shopProductId) {
        String sql = "select a.* from supply a LEFT JOIN shop_product b on a.id=b.SUPPLYID where b.id=? ";
        Object args [] = new Object[]{shopProductId};
        args = StringHelperTools.nullToString(args);
        
        LOGGER.debug("根据产品指定供应商ID获取供应商信息：" + BaseUtil.logSQL(sql, args));
        List<SupplyVo> supplyList = jdbcTemplate.query(sql,args, new RowMapper<SupplyVo>() {
            public SupplyVo mapRow(ResultSet rs, int rowNum)throws SQLException {
                SupplyVo vo = new SupplyVo();
                AutoInjection.Rs2Vo(rs, vo, null);
                return vo;
            }
        });
        if(supplyList != null && supplyList.size() > 0) {
            return supplyList.get(0);
        }
        return null;
    }

}
